package edu.zhang.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.zhang.dao.NoticeDAO;
import edu.zhang.po.Notice;

@WebServlet("/notice/NoticeController")
public class NoticeController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private NoticeDAO noticeDAO = new NoticeDAO();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String option = request.getParameter("option");
		
		if(option.equals("list")) {
			List<Notice> list = noticeDAO.getAllNotice();
			if(list!=null) {
				request.setAttribute("list", list);
				request.getRequestDispatcher("/Back/notice/listNotice.jsp").forward(request, response);
			}else {
				response.sendRedirect("error.jsp");
			}
		}else if(option.equals("insert")) {
			String title = request.getParameter("title");
			String concept = request.getParameter("concept");
			String time = request.getParameter("time");
			
			Notice notice = new Notice();
			notice.setTitle(title);
			notice.setConcept(concept);
			notice.setTime(time);
			
			int i = noticeDAO.insert(notice);
			if (i > 0) {
				request.getRequestDispatcher("NoticeController?option=list").forward(request, response);
			} else {
				response.sendRedirect("error.jsp");
			}
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
